package com.example.servlet; /**
 * @author YAO
 * @create 2021-04-19 21:18
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseIOServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决响应乱码，方案一:
//        System.out.println(response.getCharacterEncoding());  //默认 ISO-8859-1
        //设置服务器字符集为UTF-8
//        response.setCharacterEncoding("UTF-8");

        //通过响应头，设置浏览器使用UTF-8字符集
//        response.setHeader("Content-type","text/html; charset=UTF-8");

        //解决响应乱码，方案二：同时设置服务器和客户端都使用UTF-8字符集，还设置了响应头
        //此方法一定要在获取流对象之前调用才有效
        response.setContentType("text/html; charset=UTF-8");

        // 要求：往客户端回传 字符串 数据
        PrintWriter writer = response.getWriter();
        writer.println("我要努力!!");
    }


}
